package GUILayer;

import java.awt.BorderLayout;
import ModelLayer.*;
import CtrLayer.*;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

public class Main extends JFrame
{

	private JPanel contentPane;
	private JTable table_Inventory;
	private JTable table_Customer;
	private JTable table_Basket;
	private JTextField txt_searchPhone;
	private OrderCtr oC = new OrderCtr();
	private JComboBox comboBox;
	private ProductCtr pc = new ProductCtr();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					Main frame = new Main();
					frame.setVisible(true);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main()
	{
		setTitle("Gruppe 5");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 570, 694);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Basket", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(10, 443, 534, 205);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(10, 23, 514, 142);
		panel.add(scrollPane_2);
		
		table_Basket = new JTable();
		table_Basket.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Item Name", "Item Type","Unit price", "Total price", "Amount"
			}
		));
		scrollPane_2.setViewportView(table_Basket);
		
		JButton btnOrder = new JButton("Create order");
		btnOrder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				oC.updateOrder(null, null);
				//TODO
			}
		});
		btnOrder.setBounds(10, 176, 514, 23);
		panel.add(btnOrder);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Customer", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_1.setBounds(10, 11, 534, 205);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(10, 50, 514, 112);
		panel_1.add(scrollPane_1);
		
		table_Customer = new JTable();
		table_Customer.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
			},
			new String[] {
				"Name", "Address", "Zipcode", "City", "Phoneno", "Orders placed"
			}
		));
		scrollPane_1.setViewportView(table_Customer);
		
		JButton btnAddNewCustomer = new JButton("Add new customer");
		btnAddNewCustomer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AddCustomer ac = new AddCustomer();
				ac.setVisible(true);
			}
		});
		btnAddNewCustomer.setBounds(10, 173, 128, 23);
		panel_1.add(btnAddNewCustomer);
		
		txt_searchPhone = new JTextField();
		txt_searchPhone.setBounds(396, 22, 128, 20);
		panel_1.add(txt_searchPhone);
		txt_searchPhone.setText("Phone no...");
		txt_searchPhone.setColumns(10);
		
		JButton btnNewButton = new JButton("Find");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String phoneno = txt_searchPhone.getText();
				Customer c = oC.findAndAddCustomer(phoneno);
				
				addCustomer(c.getName(), c.getAddress(), c.getZipcode(), c.getCity(), c.getPhoneNo());
			}
		});
		btnNewButton.setBounds(297, 21, 89, 23);
		panel_1.add(btnNewButton);
		
		JButton btnViewCustomerOrders = new JButton("View customer orders");
		btnViewCustomerOrders.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ViewCustomerOrders vco = new ViewCustomerOrders();
				vco.setVisible(true);
			}
		});
		btnViewCustomerOrders.setBounds(385, 173, 139, 23);
		panel_1.add(btnViewCustomerOrders);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Inventory", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_2.setBounds(10, 227, 534, 205);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 52, 514, 112);
		panel_2.add(scrollPane);
		
		table_Inventory = new JTable();
		table_Inventory.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null, null, null,null, null,null},
			},
			new String[] {
				"Name", "Purchase Price", "Sales Price", "Rent price", "CountryOfOrigin", "minStock", "stock","productType", "size", "colour", "supplierid"
			}
		));
		scrollPane.setViewportView(table_Inventory);
		
		JLabel lblCategori = new JLabel("Category:");
		lblCategori.setBounds(10, 24, 514, 14);
		panel_2.add(lblCategori);
		
		comboBox = new JComboBox();
		comboBox = new JComboBox();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(comboBox.getSelectedItem().toString().equals("Clothing"))
				{
					table_Inventory.setModel(new DefaultTableModel(
							new Object[][] {
								{null, null, null, null, null, null, null, null,null, null, null},
							},
							new String[] {
								"Name", "Purchase Price", "Sales Price", "Rent price", "CountryOfOrigin", "minStock", "stock","productType", "size", "colour", "supplierid"
							}
						));	
					refreshClothing();
				}
				else if(comboBox.getSelectedItem().toString().equals("Equipment"))
				{

					table_Inventory.setModel(new DefaultTableModel(
							new Object[][] {
								{null, null, null, null, null, null, null, null,null, null, null},
							},
							new String[] {
								"Name", "Purchase Price", "Sales Price", "Rent price", "CountryOfOrigin", "minStock", "stock","productType", "type", "description", "supplierid"
							}
						));


					refreshEquipment();
				}
				else if(comboBox.getSelectedItem().toString().equals("Gun Replicas"))
				{
					table_Inventory.setModel(new DefaultTableModel(
							new Object[][] {
								{null, null, null, null, null, null, null, null,null, null},
							},
							new String[] {
								"Name", "Purchase Price", "Sales Price", "Rent price", "CountryOfOrigin", "minStock", "stock","productType", "fabric", "calibre", "supplierid"
							}
						));
					refreshGunReplicas();
				}
			}


				
			
		});
		comboBox.setBounds(80, 21, 404, 20);
		panel_2.add(comboBox);
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Clothing", "Equipment", "Gun Replicas"}));
		
		JButton btnAddItem = new JButton("New inventory item");
		btnAddItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AddInventoryItem ai = new AddInventoryItem();
				ai.setVisible(true);
			}
		});
		btnAddItem.setBounds(385, 171, 139, 23);
		panel_2.add(btnAddItem);
		
		JButton btnAddSelectedItem = new JButton("Add item to basket");
		btnAddSelectedItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int row = table_Inventory.getSelectedRow();
				
				Object selected = table_Inventory.getValueAt(row, 0);
				String itemName = selected.toString();
				
				Object selected1 = table_Inventory.getValueAt(row, 7);
				String itemType = selected1.toString();
				
				addToBasket(itemName, itemType,0, 0, 0);
				
				//TODO
				//oC.addProductToOrder(p, amount)
				
			}
		});
		btnAddSelectedItem.setBounds(10, 171, 139, 23);
		panel_2.add(btnAddSelectedItem);
	}
	
	//Tilf�jer Customer til GUI.
	
	public void addCustomer(String name, String address, int zipcode, String city, String phoneno)
	{	
		DefaultTableModel model = (DefaultTableModel) table_Customer.getModel();
		for (int i = 0; i < model.getRowCount(); i++)
		{
			model.removeRow(i);
		}
		model.addRow(
				new Object[] {
						name, address, String.valueOf(zipcode), city, phoneno
				}
				);
		
	
	}
	
	// refresh lister af produkter
	
	private void refreshClothing() {
      ArrayList<Clothing> cloth = oC.findProductClothing(comboBox.getSelectedItem().toString());
      removeRow();
      for (Clothing c : cloth)
      {

    	  ((DefaultTableModel) table_Inventory.getModel()).addRow(new Object[] { c.getName(), c.getPurchasePrice() , c.getSalesPrice() , c.getRentPrice(), c.getCountryOfOrigin(), c.getMinStock(), c.getStock(), c.getProductType(), c.getSize(), c.getColour(), c.getSupplierId() 
    			  } );
      }
	}
    	  
   private void refreshEquipment()  {
	      ArrayList<Equipment> eq = oC.findProductEquipment(comboBox.getSelectedItem().toString());
	      removeRow();
			
	      for (Equipment e : eq)
	      {
	    	  ((DefaultTableModel) table_Inventory.getModel()).addRow(new Object[] { e.getName(), e.getPurchasePrice() , e.getSalesPrice() , e.getRentPrice(), e.getCountryOfOrigin(), e.getMinStock(), e.getStock(), e.getProductType() , e.getType() , e.getDescription() , e.getSupplierId() } );
	      }
		}
   private void refreshGunReplicas() {
	   ArrayList<GunReplicas> gp = oC.findProductGunReplicas(comboBox.getSelectedItem().toString());
	removeRow();
	
   	for (GunReplicas p : gp)
 {

	  ((DefaultTableModel) table_Inventory.getModel()).addRow(new Object[] { p.getName(), p.getPurchasePrice() , p.getSalesPrice() , p.getRentPrice(), p.getCountryOfOrigin(), p.getMinStock(), p.getStock(), p.getProductType(), p.getFabric(), p.getCalibre(), p.getSupplierId()} );
}
}
   private void removeRow(){
	   DefaultTableModel model = (DefaultTableModel) table_Inventory.getModel();
	  
	   while (model.getRowCount() >= 1)
	   {
		   model.removeRow(0);
	   }
   
   }
   
   private void addToBasket(String itemName, String itemType, double unitPrice, double totalPrice, int amount)
   {
	   DefaultTableModel model = (DefaultTableModel) table_Basket.getModel();
	   model.addRow(
			   new Object[]{
					   itemName, itemType, String.valueOf(unitPrice), String.valueOf(totalPrice), String.valueOf(amount)
			   });
	   
   }
   }
      
	

